package com.xmrj.demo.controller;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.concurrent.ThreadLocalRandom;

@RestController
@RequestMapping("/manage")
//徐翔宇or温海翔
public class ManageController {
    private Map<Integer,String> crtlMap=new HashMap<>();

    @RequestMapping("/message")
    public String message(String context) {
       if(context==null||context.isEmpty()){
            return "无有效输入";
        }
        /*Map<Integer,String> map=new HashMap<>();
        this.crtlMap=map;*/
        int randomNum= ThreadLocalRandom.current().nextInt(100000,999999);
        while (this.crtlMap.containsKey(randomNum)){
            randomNum= ThreadLocalRandom.current().nextInt(100000,999999);
        }
        if (this.crtlMap.containsValue(context)){
            return "已存在。随机数为："+randomNum;
        }
        this.crtlMap.put(randomNum,context);
        return "随机数:"+randomNum+" 数值:"+context;
       /* Map<String,String> map=new HashMap<>();

        if(context==null || context.isEmpty()){
            return "无有效输入";
        }else {
            Random r=new Random();
            r.nextInt(999999);
            Iterator keys=map.keySet().iterator();
            String name=String.format("%06d",r);
            if(map.containsValue(context)==false){
                boolean flag= map.containsKey(r);
                while (flag){
                    String key = (String) keys.next();
                    flag= map.containsKey(name);
                    if (flag){
                        map.put(name,context);
                        break;
                    }else {
                        r.nextInt(999999);
                        name=String.format("%06d",r);
                    }
                }
            }else {
                String key = null;
                for(String getKey: map.keySet()){
                    if(map.get(getKey).equals(context)){
                        key = getKey;
                    }
                }
                return "输入数据已存在,6位代码为："+key;
            }

            return "添加成功，随机串："+name+"内容:"+context+"。";
        }*/
    }

    @RequestMapping("/{randomNum}")
    public String getContext(@PathVariable("randomNum") int randomNum){

        return "你输入了："+randomNum+" 对应的内容为："+crtlMap.get(randomNum);
    }
    @RequestMapping("/list")
    public List<String> list(){
        List<String> list=new ArrayList<>();

        Set<Map.Entry<Integer, String>> entries=this.crtlMap.entrySet();
        for (Map.Entry<Integer, String> entry : entries) {
            Integer randomNum =entry.getKey();
            String context=entry.getValue();
            list.add("随机串："+randomNum+" 内容:"+context);
        }
        return list;
    }

    @RequestMapping("/del")
    public String del(int id){
       crtlMap.remove(id);
       return "已删除key="+id+"的内容";
    }



}
